package com.c.ctgproject.ui.service

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.service.ServiceReturnRecordAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.service.ReturnHistoryEntity
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.InputReturnMessageDialog
import kotlinx.android.synthetic.main.activity_service_return_record.*

/**
 *作者:LiBW
 *创建日期: 2021/7/5
 *描述:回访记录 页面
 */
class ServiceReturnRecordActivity : BaseActivity(), InputReturnMessageDialog.InputReturnMessageDialogCallBack {
    private val mViewModel by lazy { ViewModelProvider(this)[ServiceReturnRecordViewModel::class.java] }
    private val suborderId by lazy { intent.getStringExtra(MyConstants.SUBORDER_ID).handlerNull() }
    private val mList = ArrayList<ReturnHistoryEntity.DataBean>()
    private val mAdapter by lazy { ServiceReturnRecordAdapter(this, mList) }
    override fun getLayout() = R.layout.activity_service_return_record
    private var mDialog: InputReturnMessageDialog? = null
    override fun onResume() {
        super.onResume()
        mViewModel.getList(suborderId)
    }

    override fun event() {
        mReturnRV.adapter = mAdapter
        mReturnRV.layoutManager = LinearLayoutManager(this)
        mViewModel.result.observe(this, { str ->
            handlerHttpResult(str!!) {
                val data = getGson().fromJson(str, ReturnHistoryEntity::class.java).data
                data?.let { list ->
                    mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })

    }

    override fun click() {
        mAddBTN.setOnClickListener {
            mDialog = InputReturnMessageDialog(suborderId, this)
            mDialog?.show(supportFragmentManager, "")
        }
    }

    override fun confirm() {
        mDialog?.dismiss()
        mViewModel.getList(suborderId)
    }
}